package com.samsung.sbackup;

import java.io.File;

import android.app.Fragment;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

public class FragmentHomeScreen extends Fragment {

	LinearLayout backupLayout;
	LinearLayout restoreLayout;
	LinearLayout scheduleLayout;
	TextView tvSchedule, tvTypeOfStorage, tvAvailable;
	View vAvailable, vUsed;
	SharedPreferences sharedPref;

	public FragmentHomeScreen() {
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.home_screen, container, false);
		backupLayout = (LinearLayout) view.findViewById(R.id.layout_backup);
		restoreLayout = (LinearLayout) view.findViewById(R.id.layout_restore);
		scheduleLayout = (LinearLayout) view.findViewById(R.id.layout_schedule);
		tvSchedule = (TextView) view.findViewById(R.id.tv_schedule_backup);
		tvTypeOfStorage = (TextView) view
				.findViewById(R.id.home_screen_tvTypeOfStorage);
		tvAvailable = (TextView) view
				.findViewById(R.id.home_screen_tvAvailable);
		vUsed = view.findViewById(R.id.home_screen_used);
		vAvailable = view.findViewById(R.id.home_screen_available);

		sharedPref = PreferenceManager
				.getDefaultSharedPreferences(getActivity());
		String folder = sharedPref.getString(
				FragmentSettingsPreference.BACKUP_PATH, "/");

		File f = new File(folder);

		if (f.getAbsolutePath().contains("ext")
				|| f.getAbsolutePath().contains("sdcard1"))
			tvTypeOfStorage.setText("External");
		else
			tvTypeOfStorage.setText("Internal");

		float available = ((float) f.getUsableSpace()) / 1024 / 1024 / 1024;
		float total = ((float) f.getTotalSpace()) / 1024 / 1024 / 1024;
		float availablePercent = (available / total);
		float usedPercent = 1 - availablePercent;

		LinearLayout.LayoutParams usedParams = new LayoutParams(0, 20,
				usedPercent);
		vUsed.setLayoutParams(usedParams);
		LinearLayout.LayoutParams availableParams = new LayoutParams(0, 20,
				availablePercent);
		vAvailable.setLayoutParams(availableParams);

		String usedSpace = String.valueOf(total - available);
		String totalSpace = String.valueOf(total);
		if (usedSpace.length() > 4)
			usedSpace = usedSpace.substring(0, 4);
		if (totalSpace.length() > 4)
			totalSpace = totalSpace.substring(0, 4);
		tvAvailable.setText(usedSpace + "/" + totalSpace + "Gb");

		backupLayout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent i = new Intent(getActivity().getApplicationContext(),
						QuickBackup.class);
				startActivity(i);
			}
		});

		restoreLayout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent i = new Intent(getActivity().getApplicationContext(),
						QuickRestore.class);
				startActivity(i);
			}
		});

		scheduleLayout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent i = new Intent(getActivity().getApplicationContext(),
						ScheduleSettings.class);
				startActivity(i);
			}
		});
		return view;
	}

	@Override
	public void onResume() {
		super.onResume();
		// listen for schedule backup state change
		SharedPreferences sharedPref = PreferenceManager
				.getDefaultSharedPreferences(getActivity()
						.getApplicationContext());
		boolean scheduleBackupState = sharedPref.getBoolean(
				FragmentSettingsPreference.SCHEDULE_BACKUP, false);
		if (scheduleBackupState)
			tvSchedule.setText("Enable");
		else
			tvSchedule.setText("Disable");
	}
}
